package diamondServer.businesslogic.resource.loader;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;

import diamondServer.businesslogic.resource.Resource;
import diamondServer.resources.Message;
import diamondServer.resources.Service;



public class URLResourceLoader
	implements ResourceLoader
{

	private final ClassLoader mClassLoader;

	/**
	 * Resources will be loaded from the default path.
	 * 
	 * @throws MalformedURLException
	 */
	public URLResourceLoader()
			throws MalformedURLException
	{
		this(getDefaultResourcesPath());
	}

	public URLResourceLoader(URL resourcesURL)
	{
		mClassLoader = new SpecialURLClassLoader(new URL[] { resourcesURL });
	}

	private static URL getDefaultResourcesPath() 
			throws MalformedURLException
	{
		File f = new File("bin" + File.separator);
		return f.toURI().toURL();
	}
	
//	@SuppressWarnings("unused")
//	private URL getDefaultResourcesPath2() 
//			throws MalformedURLException
//	{
//		File f = new File("../tests/bin" + File.separator);
//		return f.toURI().toURL();
//	}

	@Override
	public Resource load(String msgClassName, String serviceClassName)
			throws ClassNotFoundException, InstantiationException,
			IllegalAccessException, SecurityException, NoSuchMethodException,
			IllegalArgumentException, InvocationTargetException
	{
		Message msg = 
			(Message) mClassLoader.loadClass(msgClassName).newInstance();
		Service service = 
			(Service) mClassLoader.loadClass(serviceClassName).newInstance();
		
		return new Resource(msg,service);
	}

}
